package io.lckgroup.modules.lck.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.service.ShopMerchantInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * APP Do商家信息表
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-2-5 14:20:44
 */
@RestController
@RequestMapping("/app/shopMerchant")
@Api("APP商家信息表")
public class AppShopMerchantInfoController {

    @Autowired
    private ShopMerchantInfoService shopMerchantInfoService;

    /**
     * 营业打烊接口
     *
     * @param isOpen
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("openOrClose/{isOpen}")
    @ApiOperation("营业打烊接口")
    @CacheEvict(value = {"getMerchantinfo", "queryPageByShopMerchantInfoCache"}, key = "#sm.id")
    public R openOrClose(@PathVariable("isOpen") String isOpen, @AppLoginUser ShopMerchantInfoEntity sm) {

        //获取商户当前登录用户的商户信息
        ShopMerchantInfoEntity merchant = new ShopMerchantInfoEntity();
        merchant.setIsOpen(Integer.valueOf(isOpen));
        merchant.setId(sm.getId());
        //merchant.setGmtModified(new Date());
        boolean r = shopMerchantInfoService.updateById(merchant);

        if (r) {
            return R.ok().put("msg", "操作成功").put("isOpen", merchant.getIsOpen().toString());
        } else {
            return R.error(-1, "操作失败");
        }
    }

    /**
     * 修改商家海报地址
     *
     * @param map
     * @param sm
     * @return R
     */
    @AppLogin
    @PostMapping("updateImgUrl")
    @ApiOperation("修改商家海报地址")
    @CacheEvict(value = "getMerchantinfo", key = "#sm.id")
    public R updateImgUrl(@RequestBody Map<String, Object> map, @AppLoginUser ShopMerchantInfoEntity sm) {
        Long userId = sm.getUserId();
        ShopMerchantInfoEntity sce1 = new ShopMerchantInfoEntity();
        Object url = map.get("imgUrl");
        if (null != url) {
            //设置图片地址
            sce1.setImgUrl(map.get("imgUrl").toString());
            //创建where条件需要的对象
            ShopMerchantInfoEntity sce2 = new ShopMerchantInfoEntity();
            sce2.setUserId(userId);
            //创建查询条件对象
            QueryWrapper<ShopMerchantInfoEntity> q =
                    new QueryWrapper<>(sce2);
            //根据条件修改数据
            boolean update = shopMerchantInfoService.update(sce1, q);
            return update ? R.ok("修改成功") : R.error("修改失败");
        }
        return R.error("修改失败");
    }


    /**
     * 获取商家信息回显数据
     *
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getMerchantInfo")
    @ApiOperation("获商家信息回显数据")
    public R getMerchantInfoByUserId(@AppLoginUser ShopMerchantInfoEntity sm) {
        //获取当前登录用户的商户信息
        ShopMerchantInfoEntity r = sm;
        if (null != r) {
            return R.ok().put("result", r);
        } else {
            return R.error("查询失败").put("result", null);
        }

    }

    /**
     * 删除商家海报地址
     *
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("deleteImgUrl")
    @ApiOperation("删除商家海报地址")
    @CacheEvict(value = "getMerchantinfo", key = "#sm.id")
    public R deleteImgUrl(@AppLoginUser ShopMerchantInfoEntity sm) {
        //获取当前登录用户的商户信息
        ShopMerchantInfoEntity entity = new ShopMerchantInfoEntity();
        entity.setImgUrl("");
        entity.setId(sm.getId());
        boolean r = shopMerchantInfoService.updateById(entity);
        return r ? R.ok().put("msg", "删除成功") : R.error("删除失败");
    }

}
